package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.exportRFC;

import java.io.File;
import java.sql.SQLException;

import javax.swing.JFileChooser;
import javax.swing.JProgressBar;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.getRFC.GenerateXML;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.ExportRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.exportRFCPanelSubs.ExportProgressbarDialog;

public class ExportDialogThread extends Thread {

	private ExportRFCPanel exportRFCPanel;
	private JProgressBar progressBar;
private ExportProgressbarDialog dialog;
	
	public ExportDialogThread(ExportRFCPanel exportRFCPanel, ExportProgressbarDialog dialog,
			JProgressBar progressBar) {
		this.progressBar = progressBar;
		this.exportRFCPanel = exportRFCPanel;
		this.dialog = dialog;
	}

	@Override
	public void run() {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.showOpenDialog(dialog);
		File selFile =fc.getSelectedFile();
		String file = selFile.getAbsolutePath() +"/";

		GenerateXML generatexml = new GenerateXML();
		
//		for (int i = 0; i < this.exportRFCPanel.getListPanel().getRFCList()
//				.getModel().getSize(); i++) {
//			RFC rfc = (RFC) this.exportRFCPanel.getListPanel().getRFCList()
//					.getModel().getElementAt(i);
			for (int i = 0; i < this.exportRFCPanel.getListPanel().getDisplayTable().getRowCount(); i++) {
				RFC rfc = (RFC) this.exportRFCPanel.getListPanel().getDisplayTable().getElementatRow(i);
			try {
				generatexml.exportXML(rfc.getIdRFC(), file + "/xmlexport/");
				
			} catch (SQLException e1) {

				e1.printStackTrace();
			}


			this.progressBar.setValue(i+1);

		}
		
		this.dialog.dispose();
	}
}
